package com.css.bjdt_report.dataSetManage.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.sql.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.css.bjdt_report.dataSetManage.entity.DataItem;
import com.css.bjdt_report.dataSetManage.entity.DataSet;
import com.css.bjdt_report.dataSetManage.service.DataItemService;
import com.css.bjdt_report.dataSetManage.service.DataSetService;
import com.css.bjdt_report.util.CommonUtil;
import com.css.bjdt_report.util.PropertyFilter;

@Controller
@RequestMapping(value = "/dataItem")
public class DataItemController {

	@Autowired
	private DataItemService dataItemService;

	@Autowired
	private DataSetService dataSetService;

	@RequestMapping("/list")
	public String list(Model model) {
		return "/dataItem/list";
	}

	@RequestMapping("/input")
	public String input(Model model) {
		List<DataSet> dataSetList = dataSetService.query(null);
		model.addAttribute("dataSetList", dataSetList);
		return "/dataItem/input";
	}

	@ResponseBody
	@RequestMapping("/tableData")
	public Map tableData(HttpServletRequest request, @RequestParam(value = "pageNum") int pageNum,
			@RequestParam(value = "pageSize") int pageSize) {
		Criteria cnd = PropertyFilter.cri(request);

		cnd.getOrderBy().asc("setId");

		return dataItemService.queryPage(cnd, pageNum, pageSize);
		//dataItemService.queryPageForMybatis(pageNum, pageSize);
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable String id, Model model) {
		DataItem dataItem = dataItemService.querySetById(id);
		List<DataSet> dataSetList = dataSetService.query(null);
		model.addAttribute("dataSetList", dataSetList);

		model.addAttribute("dataItem", dataItem);
		return "/dataItem/input";
	}

	//修改信息集
	@RequestMapping("/update")
	public String updateSet(DataItem dataItem, Model model) {

		dataItemService.update(dataItem);
		model.addAttribute("message", "操作成功");
		return "/dataItem/input";
	}

	//检查填写的信息项是否正确
	@ResponseBody
	@RequestMapping(value = "/check/{itemChineseName}/{itemId}/{keyType}/{setId}", method = RequestMethod.POST)
	public int checkItem(@PathVariable String itemChineseName, @PathVariable String itemId, @PathVariable int keyType,
			@PathVariable String setId, Model model) {

		List<DataItem> dataItemList = dataItemService.query(null);
		for (DataItem item : dataItemList) {
			//中文名是否重复
			if (item.getItemChineseName().equals(itemChineseName) && (!item.getId().equals(itemId))) {
				model.addAttribute("message", "中文名称不能重复！");
				return 1;
			}

			if (item.getSetId().equals(setId)) {
				//是否已有主键
				if (item.getKeyType() == keyType && (!item.getId().equals(itemId))) {
					if (keyType == 1) {
						model.addAttribute("message", "已有主键");
						return 1;
					}
					//是否已有外键
					if (keyType == 2) {
						model.addAttribute("message", "已有外键");
						return 1;
					}
				}
			}
		}
		return 0;
	}

	//保存信息集
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public String insertSet(DataItem dataItem, Model model) {
		dataItem.setId(CommonUtil.getUUID());
		dataItemService.insert(dataItem);
		model.addAttribute("message", "操作成功");
		return "/dataItem/input";
	}

	//删除一个信息集
	@ResponseBody
	@RequestMapping(value = "/delete/{id}")
	public String deleteSet(@PathVariable String id) {
		dataItemService.delete(id);
		return "success";
	}
}
